#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <libgen.h>
#include <getopt.h>
#include <string.h>
#include <mosquitto.h>
#include <stdlib.h>
#include <fcntl.h>
#include <dirent.h>
#include <iniparser.h>

#define  DEF_BROKER_HOSTNAME "a1872lL9WWV.iot-as-mqtt.cn-shanghai.aliyuncs.com"
#define  DEF_BROKER_PORT "1883"
#define  DEF_BROKER_USERNAME "SHT20&a1872lL9WWV"
#define  DEF_BROKER_PASSWD "clientid12345deviceNameSHT20productKeya1872lL9WWVtimestamp=789"
#define  DEF_BROKER_CLIENTID "12345|securemode=3,signmethod=hmacsha1,timestamp=789|"
#define  DEF_BROKER_PUBTOPIC "/sys/a1872lL9WWV/${deviceName}/thing/event/property/post"
#define  DEF_PUBQOS "0"
#define  DEF_BROKER_KEEPALIVE "60"
#define  DEF_METHOD "thing.service.property.set"
#define  DEF_ID "569389422"
#define  DEF_IDENTIFIER "data"
#define  DEF_VERSION "1.0.0"

int main(void)
{

    int set_mqtt_conf(char *path_ini,char *host,int port,char *clientid,char *user,char *passwd,char *topic);
    int gain_mqtt_conf(char *path_ini, mqtt_ctx_t *mqtt);
}

int set_mqtt_conf(char *path_ini,char *host,int port,char *clientid,char *user,char *passwd,char *topic)//通过传参的形式配置参数
{
    FILE          *fp = NULL;
    dictionary    *ini = NULL;
    char          *mqtt_port;
    char           mqtt_pot[16];

    if(port)
    {
        snprintf(mqtt_pot, sizeof(mqtt_pot), "%d" , port);
        mqtt_port = mqtt_pot;
    }

    ini = iniparser_load(path_ini);

    if(ini == NULL)
    {
        printf("iniparser failure \n");
        return -1;
    }
    /*配置参数*/
    iniparser_set(ini, "aly_address:host", host);
    iniparser_set(ini, "aly_address:port", mqtt_port);
    iniparser_set(ini, "clientId:id", clientid);
    iniparser_set(ini, "user_passwd:username", user);
    iniparser_set(ini, "user_passwd:passwd", passwd);
    iniparser_set(ini, "pub_topic:topic", topic);
    /*有些参数后面再配置*/
    /*printf("path_ini: %s\n", path_ini);*/

    fp = fopen("./etc/aliyun_config.ini", "w");
    if(fp == NULL)
    {
        printf("stone: fopen error!\n");
        return -2;
    }

    iniparser_dump_ini(ini, fp);

    iniparser_freedict(ini);

    return 0;
}

int gain_mqtt_conf(char *path_ini, mqtt_ctx_t *mqtt)    //解析ini配置文件
{
    dictionary             *ini = NULL;
    const   char           *hostname;
    int                     port;
    const   char           *username;
    const   char           *passwd;
    const   char           *clientid;
    const   char           *pubTopic;
    int                     pubQos;
    int                     keepalive;

    const   char           *method;
    const   char           *id;
    const   char           *identifier;
    const   char           *version;


    if(!path_ini || !mqtt)
    {
        printf("Invalid input argument in %s!\n",__FUNCTION__);
        return -1;
    }

    /* 加载配置文件，将数据存于dictionary结构中*/
    ini = iniparser_load(path_ini);
    if(ini == NULL)
    {
        printf("iniparser_load failure!\n");
        return -2;
    }
    /*获取字符串，若未找到返回第三个参数的内容*/
    hostname = iniparser_getstring(ini,"aliyun_address:host", DEF_BROKER_HOSTNAME);
    port = iniparser_getint(ini,"aliyun_address:port", atoi(DEF_BROKER_PORT));

    username = iniparser_getstring(ini, "user_name:username", DEF_BROKER_USERNAME);
    passwd = iniparser_getstring(ini, "user_passwd:passwd", DEF_BROKER_PASSWD);
    clientid = iniparser_getstring(ini, "clientId:id",DEF_BROKER_CLIENTID );
    pubTopic = iniparser_getstring(ini, "pub_topic:topic", DEF_BROKER_PUBTOPIC);
    pubQos = iniparser_getint(ini, "aliyun_Qos:QoS", atoi(DEF_PUBQOS));
    keepalive = iniparser_getint(ini, "KEEP_ALIVE:alive",atoi(DEF_BROKER_KEEPALIVE));



    method = iniparser_getstring(ini, "f.json:method",DEF_METHOD);
    id = iniparser_getstring(ini, "f.json:id",DEF_ID);
    identifier = iniparser_getstring(ini, "f.json:identifier",DEF_IDENTIFIER);
    version = iniparser_getstring(ini, "f.json:version",DEF_VERSION);

    strncpy(mqtt->method, method, sizeof(mqtt->method));
    /*printf("mqtt->methodiiiiii: %s, strlen(method): %ld \n", mqtt->method, strlen(mqtt->method));*/

    strncpy(mqtt->id, id,   sizeof(mqtt->id));
    /*printf("mqtt->id: %s, strlen(id): %ld \n", mqtt->id, strlen(mqtt->id));*/
    strncpy(mqtt->identifier, identifier,sizeof(mqtt->identifier));
    strncpy(mqtt->version, version, sizeof(mqtt->version));
    /*  Broker settings  */
    strncpy(mqtt->hostname, hostname, sizeof(mqtt->hostname) );
    mqtt->port = port;
    printf("Use default broker server [%s:%d]\n", mqtt->hostname, mqtt->port);

    strncpy(mqtt->username, username, sizeof(mqtt->username) );
    strncpy(mqtt->passwd , passwd, sizeof(mqtt->passwd) );
    printf("Use default broker author by [%s:%s]\n", mqtt->username, mqtt->passwd);

    mqtt->keepalive= keepalive;
    printf("Use default broker keepalive timeout [%d] seconds\n", mqtt->keepalive);

    /*  Publisher settings  */
    strncpy(mqtt->pubTopic, pubTopic, sizeof(mqtt->pubTopic));
    mqtt->pubQos = pubQos;
    printf( "Use default publisher topic \"%s\" with Qos[%d]\n",mqtt->pubTopic, mqtt->pubQos);

    strncpy(mqtt->clientid,  clientid, sizeof(mqtt->clientid));

    /*释放dictionary对象/内存*/
    iniparser_freedict(ini);

    return 0;

}
